package com.itheima.service.impl;

import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import com.itheima.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author panweiwei
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增分类
     *
     * @param category 分类信息
     */
    @Override
    public void add(Category category) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(id);
        categoryMapper.add(category);
    }

    /**
     * 查询分类列表
     *
     * @return
     */
    @Override
    public List<Category> list() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        return categoryMapper.list(id);
    }


    /**
     * 获取文章详情
     *
     * @param id 文章id
     * @return 文章详情
     */
    @Override
    public Category findById(Integer id) {
        Category category = categoryMapper.findById(id);
        return category;
    }


    /**
     * 修改文章分类
     *
     * @param category
     * @return
     */
    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 删除文章分类
     *
     * @param categoryId 文章分类id
     * @return
     */
    @Override
    public void deleteById(Integer categoryId) {
        categoryMapper.deleteById(categoryId);
    }
}
